package integration;

import static mathUtils.VecMath.vecAddScaled;

import java.util.*;

public class IntegrableSystem implements Integrable {

	final int stateCountStep = 100;
	int numStates = 0;
	
	float s[][] = new float[stateCountStep][];
	float ds[][] = new float[stateCountStep][];
	
	public ArrayList<Integrable> elementList;

	public IntegrableSystem() {
		elementList = new ArrayList<Integrable>();
	}

	public void addIntegrable(Integrable a) {
		elementList.add(a);
		if (numStates + a.state().length > s.length) {
			float snew[][] = new float[s.length + stateCountStep][];
			float dsnew[][] = new float[s.length + stateCountStep][];
			for (int i = 0; i<numStates; ++i) {
				snew[i] = s[i]; dsnew[i] = ds[i];
			}
			s = snew; ds = dsnew;
		}
		for (int i = 0; i<a.state().length; ++i) {
			s[numStates+i] = a.state()[i];
			ds[numStates+i] = a.dstate()[i];
		}
		numStates += a.state().length;
	}
	
	public void removeIntegrable(Integrable a) {
		if (elementList.contains(a)) elementList.remove(a);
	}
	
	public float[][] state() {return s;}
	public float[][] dstate() {return ds;}
	
	public void findSlope() {
		for (Integrable b: elementList)
			b.findSlope();
	}
	
	public void advanceState(float dt) {
		vecAddScaled(s, ds, dt, s);
	}

}
